package com.springcloud.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 自定义填充
 * 
 * @author hzl
 *
 */

public class MyMetaObjectHandler implements MetaObjectHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(MyMetaObjectHandler.class);

	/**
	 * 注：需要将
	 * @TableField("create_time")
	 * 改为
	 *  @TableField(value = "create_time",fill = FieldFill.INSERT)
	 */
	
	// 新增填充
	@Override
	public void insertFill(MetaObject metaObject) {
		LOGGER.info("insert公共字段填充Start");
		Date now = new Date();
		this.setFieldValByName("createTime", now, metaObject);
		this.setFieldValByName("updateTime", now, metaObject);
//		this.setFieldValByName("accessUpdateTime", now, metaObject);
//		this.setFieldValByName("refreshUpdateTime", now, metaObject);

		LOGGER.info("公共字段填充end");
	}

	// 更新填充
	@Override
	public void updateFill(MetaObject metaObject) {
		LOGGER.info("update公共字段填充Start");
		Date now = new Date();
		this.setFieldValByName("updateTime", now, metaObject);

//		this.setFieldValByName("accessUpdateTime", now, metaObject);
//		this.setFieldValByName("refreshUpdateTime", now, metaObject);
		LOGGER.info("公共字段填充end");

	}
}
